use axum::Json;
use serde_json::Value;

use crate::reqwest;
// use crate::{percent_encoding::encode_url, query};

// 获取跳转验证 url
// pub async fn get_auth_url(param:Value) -> Result<String, Json<Value>>{
//     let uri = "https://open.weixin.qq.com/connect/oauth2/authorize?";

//     let appid = query::str(&param, "appid")?;
//     let url = query::str(&param, "url")?;
//     let url = encode_url(&url);

//     let scope = query::str_op(&param, "scope")?;
//     let scope = if scope.is_some() { "snsapi_userinfo" } else { "snsapi_base" };
    
//     let state = query::str_op(&param, "state")?;
//     let state = if let Some(f) = state { f } else { "STATE".to_owned() };

//     let popup = query::bool_op(&param, "popup")?;
//     let popup = if popup.is_some() { true } else { false };

//     let url = format!("{uri}appid={appid}&redirect_uri={url}&response_type=code&scope={scope}&state={state}&forcePopup={popup}#wechat_redirect");
    
//     Ok(url)
// }
pub async fn get_access_token(appid:&str, secret:&str, code:&str) -> Result<Value, Json<Value>>{
    let uri = "https://api.weixin.qq.com/sns/oauth2/access_token?";

    let url = format!("{uri}appid={appid}&secret={secret}&code={code}&grant_type=authorization_code");
    
    Ok(reqwest::get(&url).await?)
}